/**
 * Util.java
 */
package com.totobox.apps.common;

import java.io.File;
import java.text.StringCharacterIterator;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

/**
 * @author gundallove
 * @email gundallove@gmail.com
 */
public class Util {

	/**
	 * <pre>
	 * 파일 폴더 삭제
	 * @param path
	 * @return
	 * boolean
	 * </pre>
	 */
	public static boolean deleteFileDir(File path) {

		if(!path.exists()) {
			return false;
		}

		File[] files = path.listFiles();
		for (File file : files) {
			if (file.isDirectory()) {
				deleteFileDir(file);
			} else {
				file.delete();
			}
		}
		return path.delete();
	}

	/**
	 * 널처리및 공백 제거
	 * @param str
	 * @return
	 */
	public static String trim(String str) {
		return str == null ? "" : str.trim();
	}

	

	/**
	 * 네트윅이 사용 가능한 상태인가 확인
	 */
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
			Log.w(Util.LOG_TAG, "couldn't get connectivity manager");
		} else {
			NetworkInfo info = connectivity.getActiveNetworkInfo();
			if (info != null && info.getState() == NetworkInfo.State.CONNECTED) {
				Log.i(Util.LOG_TAG, "network is available");
				return true;
			}
		}
		Log.i(Util.LOG_TAG, "network is not available");
		return false;
	}
	
    /**
     * WIFI사용중?
     */
    public static boolean isWifi(Context context) 
    {
        ConnectivityManager connectivity =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	    if (connectivity == null) 
	    {
	        Log.w(Util.LOG_TAG, "couldn't get connectivity manager");
	    }
	    else 
	    {
	        NetworkInfo info = connectivity.getActiveNetworkInfo();
	        
	        if (info != null && info.getType() == ConnectivityManager.TYPE_WIFI)
	        	return true;
	    }
	    
	    return false;
    }
	
    public static int stringToInt(String str, int defaultValue) {
        try {
            return Integer.parseInt(str);
        } catch (NumberFormatException nfe) {
            return defaultValue;
        }
    }
    
    public static String getStrTime(int t){
    	
    	String rst = "00:00:00";
    	
    	if(t == 0)
			return rst;
    	
    	int h , m , s;
    	
    	h = t/3600;			//시
    	int z = t%3600;
    	m = z/60;				//분
    	s = z%60;				//초
    	
    	rst = (h<9?"0"+h:""+h);
    	rst += ":"+(m<9?"0"+m:""+m);
    	rst += ":"+(s<9?"0"+s:""+s);
    	
    	return rst;
    }
    
    public static String getHHMMSS(String time){
		
		String result = "00:00:00";
		
		if(time == null)
			return result;
		
		try{
			
			int t = Integer.parseInt(time);
			int hou , min , sec;
			
			hou = t/3600;
			int z = t%3600;
			min = z/60;
			sec = z%60;
			
			if(hou == 0) result = "00:";
			else if(hou < 10) result = "0"+hou+":";
			else result = hou+":";
			
			if(min == 0) result += "00:";
			else if(min < 10) result += "0"+min+":";
			else result += min+":";
			
			if(sec == 0) result += "00";
			else if(sec < 10) result += "0"+min;
			else result += min+"";
			
		} catch(Exception e){}
		
		return result;
	}
    
    
    public static String getTitleCut(){
    	
    	String str = "";
    	
    	return str;
    }
    
    public static int getDateDiff(String str){
    
    	int iVal = 1;
    	/*Date date1 = new Date(str);
    	Date date2 = new Date();
    	iVal = date1.compareTo(date2);*/
    	
    	return iVal;
    }
    
    public static String getCutstring(String param, int len, String tail)
    {
        if(param.length() <= len)
            return param;
        StringCharacterIterator sci = new StringCharacterIterator(param);
        StringBuffer buffer = new StringBuffer();
        buffer.append(sci.first());
        for(int i = 1; i < len; i++)
            if(i < len - 1)
            {
                buffer.append(sci.next());
            } else
            {
                char c = sci.next();
                if(c != ' ')
                    buffer.append(c);
            }

        buffer.append(tail);
        return buffer.toString();
    }
    
	private static final String LOG_TAG = Util.class.getSimpleName();
	
	
	
}
